// 文章列表页
Page({
  data: {
    articles: [], // 文章列表数据
    loading: false, // 加载状态
  },

  onLoad() {
    this.loadArticles();
  },

  // 加载文章列表
  async loadArticles() {
    if (this.data.loading) return;
    
    this.setData({ loading: true });
    
    try {
      const { result } = await wx.cloud.callFunction({
        name: 'getArticles',
        data: { 
          type: 'list',
          page: 1,
          pageSize: 10,
          orderBy: 'createTime',
          order: 'desc'
        }
      });

      if (result.code === 0) {
        this.setData({ 
          articles: result.data.list,
          loading: false 
        });
      } else {
        throw new Error(result.message || '获取文章列表失败');
      }
    } catch (err) {
      console.error('加载文章失败:', err);
      this.setData({ loading: false });
      wx.showToast({ title: '加载失败', icon: 'none' });
    }
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadArticles().finally(() => {
      wx.stopPullDownRefresh();
    });
  },

  // 跳转到文章详情
  goToDetail(e) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/article/detail/index?id=${id}`
    });
  }
});